package zzps.exeed.kernel.convert.impl;

import zzps.exeed.kernel.convert.Converter;

import java.time.*;
import java.time.temporal.TemporalAccessor;

public class TemporalAccessorConverter implements Converter<TemporalAccessor> {
    private final Class<? extends TemporalAccessor> targetType;

    public TemporalAccessorConverter(Class<? extends TemporalAccessor> targetType) {
        this.targetType = targetType;
    }

    @Override
    public TemporalAccessor convert(Object value) {
        String text = toStr(value);
        if (Instant.class == targetType){
            return Instant.parse(text);
        }
        else if (LocalDate.class == this.targetType) {
            return LocalDate.parse(text);
        }
        else if (LocalTime.class == this.targetType) {
            return LocalTime.parse(text);
        }
        else if (LocalDateTime.class == this.targetType) {
            return LocalDateTime.parse(text);
        }
        else if (ZonedDateTime.class == this.targetType) {
            return ZonedDateTime.parse(text);
        }
        else if (OffsetDateTime.class == this.targetType) {
            return OffsetDateTime.parse(text);
        }
        else if (OffsetTime.class == this.targetType) {
            return OffsetTime.parse(text);
        }
        else if (YearMonth.class == this.targetType) {
            return YearMonth.parse(text);
        }
        else if (MonthDay.class == this.targetType) {
            return MonthDay.parse(text);
        }
        return null;
    }
}
